package com.byxy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byxy.entity.Forum;
import com.byxy.entity.Reply;
import com.byxy.entity.Tab;
import com.byxy.entity.Tip;
import com.byxy.entity.User;
import com.byxy.service.ForumService;
import com.byxy.service.ReplyService;
import com.byxy.service.TabService;
import com.byxy.service.TipService;
import com.byxy.service.UserService;

@Controller
public class TipController {
	@Autowired
	private TipService tipService;

	@Autowired
	private UserService userService;

	@Autowired
	private TabService tabService;

	@Autowired
	private ForumService forumService;

	@Autowired
	private ReplyService replyService;

	@RequestMapping("toMainPage.do")
	public String toMainPage(Model model) {
		List<Tip> tips = tipService.findAll();
		Tab t = new Tab();
		for (Tip tip2 : tips) {
			User u = userService.get(tip2.getUser().getUserId());
			tip2.getUser().setUserName(u.getUserName());
			tip2.getUser().setUserNick(u.getUserNick());
			t = tabService.get(tip2.getTab().getTabId());
			Forum f = forumService.get(t.getForum().getForumId());
			tip2.getTab().setForum(f);
			tip2.getTab().setTabName(t.getTabName());
		}
		model.addAttribute("tab", t);
		model.addAttribute("tips", tips);
		return "main";
	}

	@RequestMapping("showTip.do")
	public String showTip(Model model, @RequestParam("tipId") Integer tipId) {
		Tip tip = tipService.get(tipId);
		User u = userService.get(tip.getUser().getUserId());
		tip.getUser().setUserName(u.getUserName());
		tip.getUser().setUserNick(u.getUserNick());
		Tab t = tabService.get(tip.getTab().getTabId());
		Forum f = forumService.get(t.getForum().getForumId());
		tip.getTab().setForum(f);
		tip.getTab().setTabName(t.getTabName());
		model.addAttribute("tip", tip);
		List<Reply> replies = replyService.findAll();
		for (Reply reply : replies) {
			reply.setTip(tip);
			reply.setUser(u);
		}
		model.addAttribute("replies", replies);
		return "tipContent";
	}

	@RequestMapping("toLoginPage.do")
	public String toLoginPage(@RequestParam("tipId") Integer tipId, Model model) {
		return "login";

	}

	@GetMapping("toPublishTip.do")
	public String toPublishTip(Model model) {
		model.addAttribute("forums", forumService.findAll());
		model.addAttribute("tabs", tabService.findAll());
		return "publishTip";
	}

	@RequestMapping("publishNewTip.do")
	public String publishNewTip(@RequestParam("tip_title") String tipTitle,
			@RequestParam("selectedForumId") Integer forumId, @RequestParam("tab_id") Integer tabId,
			@RequestParam("tip_content") String tipContent, HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("USER");
		tipService.add(tipTitle, tipContent, tabId, u.getUserId());
		return "redirect:toMainPage.do";
	}

	@GetMapping("toTipManagePage.do")
	public String toTipManage(Model model) {
		model.addAttribute("tips", tipService.findAll());
		return "tipManage";
	}

	@GetMapping("toModifyTipPage.do")
	public String toModifyTipPage(Model model, @RequestParam("tipId") Integer tipId) {
		Tip tip = tipService.get(tipId);
		User user = userService.get(tip.getUser().getUserId());
		tip.getUser().setUserName(user.getUserName());
		tip.getUser().setUserNick(user.getUserNick());
		model.addAttribute("forums", forumService.findAll());
		model.addAttribute("tip", tip);
		model.addAttribute("tabs", tabService.findAll());
		return "modifyTip";
	}

	@RequestMapping("modifyTip.do")
	public String modifyTip(@RequestParam("tip_id") Integer tipId, @RequestParam("selectedForumId") Integer forumId,
			@RequestParam("selectedTabId") Integer tabId, @RequestParam("tip_title") String tipTitle,
			@RequestParam("tip_content") String tipContent) {
		tabService.updateForumId(forumId, tabId);
		tipService.update(tipTitle, tabId, tipContent, tipId);
		return "redirect:toMainPage.do";
	}

	@RequestMapping("changeTipStatus.do")
	public String ChangeTipStatus(@RequestParam("tipId") Integer tipId, @RequestParam("opr") Integer opr) {
		Tip tip = tipService.get(tipId);
		if (opr == 3) {
			tipService.changeTipStatus(tip.getTipIsDeleted(), 1, tipId);
		} else if (opr == 1) {
			tipService.changeTipStatus(1, tip.getTipIsKnot(), tipId);
		} else if (opr == 2) {
			tipService.changeTipStatus(0, tip.getTipIsKnot(), tipId);
		} else if (opr == 4) {
			tipService.changeTipStatus(tip.getTipIsDeleted(), 0, tipId);
		}
		return "redirect:toTipManagePage.do";
	}

}
